package com.zfc.mymall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zfc.mymall.product.entity.CategoryEntity;
import com.zfc.mymall.product.service.CategoryService;
import com.zfc.common.utils.PageUtils;
import com.zfc.common.utils.R;


/**
 * 商品三级分类
 *
 * @author zfcer
 * @email scmail@mail.ustc.edu.cn
 * @date 2021-12-08 11:35:41
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
  @Autowired
  private CategoryService categoryService;

  /**
   * 查询所有分类，组装成tree
   */
  @GetMapping("/list/tree")
  public R list() {
    List<CategoryEntity> categoryEntityList = categoryService.listWithTree();
    return R.ok().put("data", categoryEntityList);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{catId}")
  public R info(@PathVariable("catId") Long catId) {
    CategoryEntity category = categoryService.getById(catId);

    return R.ok().put("category", category);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  public R save(@RequestBody CategoryEntity category) {
    categoryService.save(category);

    return R.ok();
  }

  /**
   * 批量修改层级
   */
  @RequestMapping("/update/sort")
  public R updateSort(@RequestBody CategoryEntity[] category){
    categoryService.updateBatchById(Arrays.asList(category));
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  public R update(@RequestBody CategoryEntity category) {
    categoryService.updateCascade(category);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  public R delete(@RequestBody Long[] catIds) {
    // 删除 catgegories
    categoryService.removeMenuByIds(Arrays.asList(catIds));

    return R.ok();
  }

}
